package com.wlts.wzy.LmOrderModule;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.*;
import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.DYOrderModule.entity.DyOrders;
import com.wlts.wzy.DYOrderModule.service.IDyOrdersService;
import com.wlts.wzy.KSOrderModule.entity.KsExpressCode;
import com.wlts.wzy.KSOrderModule.entity.KsOrders;
import com.wlts.wzy.KSOrderModule.service.IKsOrdersService;
import com.wlts.wzy.LMGoodsModule.entity.LmAddress;
import com.wlts.wzy.LMGoodsModule.service.ILmAddressService;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.api.request.lm.GetLmAddressPageRequest;
import com.wlts.wzy.api.request.lm.GetLmGoodsInventoryRequest;
import com.wlts.wzy.api.request.lm.GetLmOrderExpresseRequest;
import com.wlts.wzy.api.request.lm.GetLmOrderRequest;
import com.wlts.wzy.api.response.lm.GetLmOrderExpressResponse;
import com.wlts.wzy.api.response.lm.GetLmOrderResponse;
import com.wlts.wzy.configBean.LmConfig.LmConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.CopyUtils;
import com.wlts.wzy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.wlts.wzy.configBean.LmConfig.LmConfig.client;

/**
 * <p>
 * 区域表 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-01-30
 */
@Api(tags = "Lm订单 前端控制器")
@RestController
@RequestMapping("/LMGoodsModule/lm-address")
public class LmOrderController {
    @Autowired
    private IKsOrdersService ksOrdersService;
    @Autowired
    private IDyOrdersService dyOrdersService;

    @ApiOperation(value = "获得LM订单详情")
    @RequestMapping(value = "GetLmOrderByOid", method = RequestMethod.POST)
    public Result<GetLmOrderResponse> GetLmOrderByOid(@RequestBody GetLmOrderRequest getLmOrderRequest) throws ClientException {

        Set<String> oidset = new HashSet<>();

        oidset.add(getLmOrderRequest.getOid());

        QueryOrderListRequest request = new QueryOrderListRequest();

        request.setBizId(LmConfig.LZ_BIZ_ID);
        request.setAccountType("ANONY");
        request.setThirdPartyUserId(getLmOrderRequest.getSellerId());
        request.setBizUid(request.getThirdPartyUserId());
        request.setPageSize(10L);
        request.setPageNumber(1L);
        JSONObject filterOption = new JSONObject();

//        filterOption.put("orderStatus", 2);//"orderStatus":"12=待⽀付，2=已⽀付，4=已退款关闭，6=交易成功，8=被淘宝 关闭 ",
//        filterOption.put("logisticsStatus", 2);//1=未发货 -> 等待卖家发货 2=已发货 -> 等待买家确 认收货 3=已收货 -> 交易成功 4=已经退货 -> 交易失败 5=部分收货 -> 交易成功 6= 部分发货中 8=还未创建物流订单"
//        filterOption.put("enableStatus", 0);
//        filterOption.put("lmOrderList", oidset);
        filterOption.put("channelOrderIdList", oidset);

        request.setFilterOption(filterOption.toJSONString());

        QueryOrderListResponse response = client.getAcsResponse(request);

        System.out.println(JSON.toJSONString(response));

        if(response.getLmOrderList().size()>0){

            GetLmOrderResponse  lmOrderResponse=new GetLmOrderResponse();

            CopyUtils.copyProperties(response.getLmOrderList().get(0),lmOrderResponse);

            System.out.println(response.getLmOrderList().get(0).getTbOrderId());

            lmOrderResponse.setTbOrderId(response.getLmOrderList().get(0).getTbOrderId().toString());


            MyLambdaQueryWrapper<DyOrders> queryWrapper=new MyLambdaQueryWrapper<>();

            queryWrapper.eq(DyOrders::getOrderId,getLmOrderRequest.getOid());

            queryWrapper.eq(DyOrders::getShopId,getLmOrderRequest.getSellerId());

            DyOrders dyOrders = dyOrdersService.getOne(queryWrapper);

            if(dyOrders!=null){

                if(StringUtils.isNotEmpty(lmOrderResponse.getLmOrderId())&&lmOrderResponse.getLmOrderId()!=0){
                    dyOrders.setLmOrderId(lmOrderResponse.getLmOrderId()+"");
                }

                if(StringUtils.isNotEmpty(lmOrderResponse.getTbOrderId())){
                    dyOrders.setTbOrderId(lmOrderResponse.getTbOrderId());
                }
                dyOrdersService.updateById(dyOrders);

            }else {

                MyLambdaQueryWrapper<KsOrders> ksWrapper=new MyLambdaQueryWrapper<>();

                ksWrapper.eq(KsOrders::getOid,getLmOrderRequest.getOid());

                ksWrapper.eq(KsOrders::getSellerId,getLmOrderRequest.getSellerId());

                KsOrders ksOrders = ksOrdersService.getOne(ksWrapper);

                if(ksOrders!=null){

                    if(StringUtils.isNotEmpty(lmOrderResponse.getLmOrderId())&&lmOrderResponse.getLmOrderId()!=0){
                        ksOrders.setLmOrderId(lmOrderResponse.getLmOrderId()+"");
                    }

                    if(StringUtils.isNotEmpty(lmOrderResponse.getTbOrderId())){
                        ksOrders.setTbOrderId(lmOrderResponse.getTbOrderId());
                    }
                    ksOrdersService.updateById(ksOrders);
                }
            }

            return Result.buildResult(lmOrderResponse);

        }

        return Result.buildResult(500,"Lm订单不存在");

    }

    @ApiOperation(value = "查看订单快递单号")
    @RequestMapping(value = "GetLmOrderExpressByOid", method = RequestMethod.POST)
    public Result<GetLmOrderExpressResponse> GetLmOrderExpressByOid(@RequestBody GetLmOrderExpresseRequest getLmOrderExpresseRequest) throws ClientException {

        QueryLogisticsRequest request = new QueryLogisticsRequest();
        request.setBizId(LmConfig.LZ_BIZ_ID);
        request.setAccountType("ANONY");
        request.setThirdPartyUserId(getLmOrderExpresseRequest.getSellerId() + "");
        request.setBizUid(request.getThirdPartyUserId());
        request.setLmOrderId(getLmOrderExpresseRequest.getLmOrderId());

        try {
            QueryLogisticsResponse response = client.getAcsResponse(request);

            System.out.println(JSON.toJSONString(response));

            if (response.getCode().equals("SUCCESS")) {
                List<QueryLogisticsResponse.DataItem> data = response.getData();
                QueryLogisticsResponse.DataItem item = data.get(0);
                if(item!=null){
                    GetLmOrderExpressResponse expressResponse=new GetLmOrderExpressResponse();
                    CopyUtils.copyProperties(item,expressResponse);
                    return Result.buildResult(expressResponse);
                }

            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.buildResult(500,"快递单号不存在！");

    }


}
